﻿// HSS.Forms.CustomStateTreeView.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       CustomStateTreeView.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Forms
{
	#region Using Directives
	using System;
	using System.Collections.Generic;
	using System.Text;
	#endregion

	#region CustomStateTreeView
	/// <summary>
	/// A TreeView that delegates node state changes to an IStateTreeNode
	/// that supports showing custom state images.
	/// </summary>
	[System.ComponentModel.Designer(typeof(Design.CustomStateTreeViewDesigner))]
	public class CustomStateTreeView : System.Windows.Forms.TreeView
	{
		#region Constructor
		/// <summary>
		/// Constructor
		/// </summary>
		public CustomStateTreeView()
		{
		}
		#endregion

		#region Overrides
		/// <summary>
		/// Overrides System.Windows.Forms.Control.OnHandleCreated(System.EventArgs).
		/// </summary>
		/// <param name="e">An System.EventArgs that contains the event data.</param>
		protected override void OnHandleCreated(EventArgs e)
		{
			base.OnHandleCreated(e);
			base.StateImageList = new System.Windows.Forms.ImageList();
			this.CreateStateImages();
		}
		/// <summary>
		/// Raises the System.Windows.Forms.Control.MouseDown event.
		/// </summary>
		/// <param name="e">A System.Windows.Forms.MouseEventArgs that contains the event data.</param>
		protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
		{
			System.Windows.Forms.TreeViewHitTestInfo info = this.HitTest(e.X, e.Y);
			if (info.Node != null && info.Location.ToString() == "StateImage")
				OnCustomCheck(info.Node, System.Windows.Forms.TreeViewAction.ByMouse);
			base.OnMouseDown(e);
		}
		/// <summary>
		/// Raises the System.Windows.Forms.Control.KeyDown event.
		/// </summary>
		/// <param name="e">A System.Windows.Forms.KeyEventArgs that contains the event data.</param>
		protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
		{
			switch (e.KeyCode)
			{
				case System.Windows.Forms.Keys.Space:
					if (this.StateImageList != null && this.SelectedNode != null)
						OnCustomCheck(SelectedNode, System.Windows.Forms.TreeViewAction.ByKeyboard);
					e.Handled = true;
					break;
			}
			base.OnKeyDown(e);
		}
		#endregion

		#region Methods
		/// <summary>
		/// Allows for the creation of a custom state image list
		/// </summary>
		protected virtual void CreateStateImages()
		{
		}
		/// <summary>
		/// When overridden allows for custom handling
		/// </summary>
		/// <param name="node">The node being checked or unchecked</param>
		/// <param name="action">The action that caused this event</param>
		protected virtual void OnCustomCheck(System.Windows.Forms.TreeNode node, System.Windows.Forms.TreeViewAction action)
		{
			System.Windows.Forms.TreeViewCancelEventArgs e = new System.Windows.Forms.TreeViewCancelEventArgs(node, false, action);
			OnBeforeCheck(e);
			if (e.Cancel) return;

			if (node is IStateTreeNode)
				((IStateTreeNode)node).UpdateState(e);

			OnAfterCheck(new System.Windows.Forms.TreeViewEventArgs(node, action));

		}
		#endregion
	}
	#endregion
}